<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>组件嵌套组件（双向）</title>
</head>
<body>

<!--template for the form-->
<template id="form">
  <div>
    <label for="name">What's your name?</label>
    <input v-bind:value="value" type="text" id="name" v-on:input="onInput">
  </div>
</template>

<!--template for saying hello-->
<template id="hello">
  <h1>{{msg}} {{user}}</h1>
</template>

<!--template for the greetings-->
<template id="greetings">
  <div>
    <!-- bind a user model to the form  input  component and
    emit the input event every time the user types in the input box.  -->
    <form-component v-model="user"></form-component>
    <!--上行代码是下行的语法糖
    <form-component :value="user" @input="user = arguments[0]"></form-component>
    -->
    <hello-component :user="user"></hello-component>
  </div>
</template>

<div id="app">
  <greetings-component></greetings-component>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
<script>
  Vue.component('form-component', {
    template: '#form',
    props: ['value'],
    methods: {
      onInput: function (event) {
        this.$emit('input', event.target.value)
      }
    }
  });

  Vue.component('hello-component', {
    template: '#hello',
    data: function () {
      return {
        msg: 'Hello'
      }
    },
    props: ['user']
  });

  Vue.component('greetings-component', {
    template: '#greetings',
    data: function () {
      return {
        user: 'hero'
      }
    }
  });

  new Vue({
    el: '#app'
  });
</script>
</body>
</html>
